home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / 2DLIFE.C next >
C/C++ Source or Header  |  1993-04-05  |  3KB  |  112 lines

  1. /*
  2. ** A quick "life" (2-d cellular automaton) implementation done in Turbo C 2.0 
  3. ** on the spur-of-the-moment by Jonathan Guthrie 9/20/1992 and donated to the 
  4. ** public domain.
  5. **
  6. ** In keeping with the guidelines of the C_ECHO, this program has been tested,
  7. ** and does seem to operate properly.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. #ifndef random
  16.  #define random(num) (int)(((long)rand()*(num))/RAND_MAX)
  17. #endif
  18.  
  19. /*
  20. **  From VIDPORT.C, also in SNIPPETS
  21. */
  22.  
  23. void GotoXY(int col, int row);
  24. void ClrScrn(int vattrib);
  25.  
  26. #ifndef randomize
  27.  #define randomize() srand(((unsigned int)time(NULL))|1)
  28. #endif
  29.  
  30. #define     ROWS        24
  31. #define     COLS        80
  32. #define     GENERATIONS 10
  33.  
  34. int civ1[ROWS][COLS], civ2[ROWS][COLS];
  35.  
  36. void update_generation(int old[ROWS][COLS], int new[ROWS][COLS])
  37. {
  38.       int i, j, count;
  39.  
  40.       for (i = 0; i < ROWS; ++i)
  41.       {
  42.             for (j = 0; j < COLS; ++j)
  43.             {
  44.                   count = old[(i + ROWS - 1) % ROWS][(j + COLS - 1) % COLS] + 
  45.                         old[(i + ROWS - 1) % ROWS][j] +
  46.                         old[(i + ROWS - 1) % ROWS][(j + 1) % COLS] +
  47.                         old[i][(j + COLS - 1) % COLS] +
  48.                         old[i][(j + 1) % COLS] +
  49.                         old[(i + 1) % ROWS][(j + COLS - 1) % COLS] +
  50.                         old[(i + 1) % ROWS][j] +
  51.                         old[(i + 1) % ROWS][(j + 1) % COLS];
  52.  
  53.                   switch(count)
  54.                   {
  55.                   case 0:
  56.                   case 1:
  57.                   case 4:
  58.                   case 5:
  59.                   case 6:
  60.                   case 7:
  61.                   case 8:
  62.                         new[i][j] = 0;
  63.                         break;
  64.  
  65.                   case 2:
  66.                         new[i][j] = old[i][j];
  67.                         break;
  68.  
  69.                   case 3:
  70.                         new[i][j] = 1;
  71.                         break;
  72.                   }
  73.  
  74.                   GotoXY(j+1, i+1);
  75.                   putch(new[i][j] ? '*' : ' ');
  76.             } 
  77.       }
  78. }
  79.  
  80.  
  81. void initialize(void)
  82. {
  83.       int i, j;
  84.  
  85.       randomize();
  86.       ClrScrn(7);
  87.  
  88.       for (i = 0; i < ROWS; ++i)
  89.       {
  90.             for (j = 0; j < COLS; ++j)
  91.             {
  92.                   civ1[i][j] = random(2); 
  93.                   GotoXY(j+1, i+1);
  94.                   putch(civ1[i][j] ? '*' : ' ');
  95.             } 
  96.       }
  97. }
  98.  
  99.  
  100. int main(void)
  101. {
  102.       int i;
  103.  
  104.       initialize();
  105.       for (i = 0; i < GENERATIONS; ++i)
  106.       {
  107.             update_generation(civ1, civ2);
  108.             update_generation(civ2, civ1);
  109.       }
  110.       return EXIT_SUCCESS;
  111. }
  112.